require 'rubygems'
require 'fox16'

require 'JournalXMLib.rb'
#require 'uploader.rb'

include Fox

class JournalWindow < FXMainWindow

  include Responder
  
  journal = Journal
  
  def initialize(app)

    super(app, "Journal Viewer", :opts => DECOR_ALL, :width => 850, :height => 600)

	# Make menu bar
    menubar = FXMenuBar.new(self, LAYOUT_SIDE_TOP|LAYOUT_FILL_X)
	
	# Status bar
    statusbar = FXStatusBar.new(self,
      LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|STATUSBAR_WITH_DRAGCORNER)
  
    # Docking sites
    topDockSite = FXDockSite.new(self, LAYOUT_SIDE_TOP|LAYOUT_FILL_X)
    FXDockSite.new(self, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X)
    FXDockSite.new(self, LAYOUT_SIDE_LEFT|LAYOUT_FILL_Y)
    FXDockSite.new(self, LAYOUT_SIDE_RIGHT|LAYOUT_FILL_Y)

    # Splitter
    splitter = FXSplitter.new(self, (LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y| SPLITTER_TRACKING|SPLITTER_VERTICAL|SPLITTER_REVERSED))
	
	# File menu
    filemenu = FXMenuPane.new(self)
    FXMenuTitle.new(menubar, "&File", nil, filemenu)
	FXMenuCommand.new(filemenu, "&Open...\tCtl-O\tOpen image file.").connect(SEL_COMMAND, method(:onCmdOpen))
    FXMenuCommand.new(filemenu, "&Save...\tCtl-S\tSave image file.").connect(SEL_COMMAND, method(:onCmdSave))
	FXMenuCommand.new(filemenu, "&Upload...\tCtl-U\tUpload journal to flickr.").connect(SEL_COMMAND, method(:onCmdUpdate))
    FXMenuCommand.new(filemenu, "&Quit\tCtl-Q").connect(SEL_COMMAND, method(:onCmdQuit))
	
	# Sunken border for image widget
    imagebox = FXHorizontalFrame.new(splitter,
      FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y,
      :padLeft => 0, :padRight => 0, :padTop => 0, :padBottom => 0)
  
    # Make image widget
    @imageview = FXImageView.new(imagebox, :opts => LAYOUT_FILL_X|LAYOUT_FILL_Y)
    
	@filebox = FXHorizontalFrame.new(splitter,
      LAYOUT_FILL_X|LAYOUT_FILL_Y,
      :padLeft => 0, :padRight => 0, :padTop => 0, :padBottom => 0)
	
    fileframe = FXVerticalFrame.new(@filebox,
      FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y,
      :padLeft => 0, :padRight => 0, :padTop => 0, :padBottom => 0, :hSpacing => 0, :vSpacing => 0)
	
    @header1 = FXHeader.new(fileframe, 
      :opts => HEADER_BUTTON|HEADER_RESIZE|FRAME_RAISED|FRAME_THICK|LAYOUT_FILL_X)
    @header1.connect(SEL_CHANGED) do |sender, sel, which|
      @lists[which].width = @header1.getItemSize(which)
    end
    @header1.connect(SEL_COMMAND) do |sender, sel, which|
      @lists[which].numItems.times do |i|
        @lists[which].selectItem(i)
      end
    end
  
    @header1.appendItem("Trip Name", nil, 150)
    @header1.appendItem("Destination", nil, 150)
    @header1.appendItem("Country", nil, 150)
	@header1.appendItem("City", nil, 150)
    @header1.appendItem("Dates", nil, 91)
    @header1.appendItem("Photos", nil, 147)
 
    panes = FXHorizontalFrame.new(fileframe, FRAME_SUNKEN|FRAME_THICK|LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|LAYOUT_FILL_Y,
      :padLeft => 0, :padRight => 0, :padTop => 0, :padBottom => 0,
      :hSpacing => 0, :vSpacing => 0)

    @lists = []
    @lists.push(FXList.new(panes, :opts => LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH|LIST_SINGLESELECT, :width => 150))
    @lists.push(FXList.new(panes, :opts => LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH|LIST_SINGLESELECT, :width => 150))
    @lists.push(FXList.new(panes, :opts => LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH|LIST_SINGLESELECT, :width => 150))
    @lists.push(FXList.new(panes, :opts => LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH|LIST_SINGLESELECT, :width => 150))
    @lists.push(FXList.new(panes, :opts => LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH|LIST_SINGLESELECT, :width => 91))
    @lists.push(FXList.new(panes, :opts => LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH|LIST_SINGLESELECT, :width => 147))

	@lists[0].connect(SEL_COMMAND, method(:onCmdSel))
	@lists[1].connect(SEL_COMMAND, method(:onCmdSel))
	@lists[2].connect(SEL_COMMAND, method(:onCmdSel))
	@lists[3].connect(SEL_COMMAND, method(:onCmdSel))
	@lists[4].connect(SEL_COMMAND, method(:onCmdSel))
	@lists[5].connect(SEL_COMMAND, method(:onCmdSel))
	
    @lists[0].backColor = FXRGB(240, 240, 255)
    @lists[1].backColor = FXRGB(240, 255, 240)
    @lists[2].backColor = FXRGB(240, 240, 255)
	@lists[3].backColor = FXRGB(240, 255, 240)
    @lists[4].backColor = FXRGB(240, 240, 255)
	@lists[5].backColor = FXRGB(255, 240, 240)

    @lists[0].appendItem("All")
	@lists[1].appendItem("All")
	@lists[2].appendItem("All")
	@lists[3].appendItem("All")	
	@lists[4].appendItem("All")
	@lists[5].appendItem("None")
     
    @filename = "untitled"
    @preferredFileFilter = 0
	
  end
  
  def loadJournal(filename)
	journal = Journal.new(filename)
	
  end
  
  def hasExtension?(filename, ext)
    File.basename(filename, ext) != File.basename(filename)
  end
  
  def loadImage(file)
    img = nil
    if hasExtension?(file, ".gif")
      img = FXGIFImage.new(getApp(), nil, IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP)
    elsif hasExtension?(file, ".bmp")
      img = FXBMPImage.new(getApp(), nil, IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP)
    elsif hasExtension?(file, ".xpm")
      img = FXXPMImage.new(getApp(), nil, IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP)
    #elsif hasExtension?(file, ".png")
      #img = FXPNGImage.new(getApp(), nil, IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP)
    elsif hasExtension?(file, ".jpg")
      img = FXJPGImage.new(getApp(), nil, IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP)
    elsif hasExtension?(file, ".pcx")
      img = FXPCXImage.new(getApp(), nil, IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP)
    elsif hasExtension?(file, ".tif")
      img = FXTIFImage.new(getApp(), nil, IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP)
    elsif hasExtension?(file, ".tga")
      img = FXTGAImage.new(getApp(), nil, IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP)
    elsif hasExtension?(file, ".ico")
      img = FXICOImage.new(getApp(), nil, IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP)
    end

    # Perhaps failed?
    if img.nil?
      FXMessageBox.error(self, MBOX_OK, "Error loading image",
        "Unsupported image type: #{file}")
      return
    end

    # Load it...
    getApp().beginWaitCursor do
      FXFileStream.open(file, FXStreamLoad) { |stream| img.loadPixels(stream) }
      img.create
      @imageview.image = img
    end
  end
  
   # Open a new file
  def onCmdOpen(sender, sel, ptr)
    openDialog = FXFileDialog.new(self, "Open Image")
    openDialog.filename = @filename
    patterns = ["TravelJournal (*.xml)", "All Files (*)"]
    openDialog.patternList = patterns
    openDialog.currentPattern = @preferredFileFilter
    if openDialog.execute != 0
	  @filename = openDialog.filename
      loadJournal(@filename)
    end
	return 1
  end

  # Save this file
  def onCmdSave(sender, sel, ptr)
    journal.WriteToFile(journal.document)
	return 1
  end
  
  def onCmdUpdate(sender, sel, ptr)
	uploader = Uploader.new("token")
	uploader.uploadJournal(journal)
	journal.WriteToFile(journal.document)
	UploadDialog.new(self).execute
	return 1
  end
  
  def onCmdSel(sender, sel, ptr)
	print @lists[0].currentItem
	print @lists[1].currentItem
	print @lists[2].currentItem
	print @lists[3].currentItem
	print @lists[4].currentItem
	print @lists[5].currentItem
	
  end
  
  def onCmdQuit(sender, sel, ptr)
    getApp().reg().writeIntEntry("SETTINGS", "x", x)
    getApp().reg().writeIntEntry("SETTINGS", "y", y)
    getApp().reg().writeIntEntry("SETTINGS", "width", width)
    getApp().reg().writeIntEntry("SETTINGS", "height", height)
    getApp().exit(0)
  end
  
  # Create and show window
  def create
    # Get size, etc. from registry
    xx = getApp().reg().readIntEntry("SETTINGS", "x", 0)
    yy = getApp().reg().readIntEntry("SETTINGS", "y", 0)
    ww = getApp().reg().readIntEntry("SETTINGS", "width", 850)
    hh = getApp().reg().readIntEntry("SETTINGS", "height", 600)
    position(xx, yy, ww, hh)
    super   
    show(PLACEMENT_SCREEN)
  end  
end

class UploadDialog < FXDialogBox

  def initialize(owner)
    super(owner, "Upload", DECOR_TITLE|DECOR_BORDER)
	horframe = FXHorizontalFrame.new(self, LAYOUT_SIDE_TOP | LAYOUT_FILL_X)
	FXLabel.new(horframe, "The journal has been uploaded to www.flick.com/something!!!", nil, LAYOUT_SIDE_TOP | JUSTIFY_LEFT)
	buttons = FXHorizontalFrame.new(self,
      LAYOUT_SIDE_BOTTOM|FRAME_NONE|LAYOUT_FILL_X|PACK_UNIFORM_WIDTH,
      :padLeft => 80, :padRight => 80, :padTop => 10, :padBottom => 5)
	accept = FXButton.new(buttons, "&Okie Dokie", nil, self, ID_ACCEPT,
      FRAME_RAISED|FRAME_THICK|LAYOUT_CENTER_X|LAYOUT_CENTER_Y)	
  end
end

if __FILE__ == $0
  # Make application
  application = FXApp.new("JournalViewer", "Flcikr")

  # Make window
  window = JournalWindow.new(application)

  # Handle interrupts to terminate program gracefully
  application.addSignal("SIGINT", window.method(:onCmdQuit))

  # Create it
  application.create

  # Passed image file?
  if ARGV.length > 0
    window.loadImage(ARGV[0])
  end

  # Run
  application.run
end
